package com.io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

/**
 * 利用文件输入输出流编写一个实现文件拷贝的程序，源文件名和目标文件名通过控制台输入。
 *
 * @author: Jovi
 * @createTime: 2017-06-09 14:04
 **/
public class CopyFile {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int date = 0;
        System.out.println("Please input you want to copy the source file：");
        String file1 = sc.next();
        System.out.println("Please input you want to copy the destination file");
        String file2 = sc.next();
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(file1);
            fos = new FileOutputStream(file2);
            while ((date = fis.read()) != -1) {
                fos.write(date);
            }
            System.out.println("Copy success!");
        } catch (Exception e) {
            System.out.println("Source file is not found");
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
